﻿using MaterialDesignThemes.Wpf;
using Morin.Services;
using Morin.Shared.Models;
using Stylet;
using System.ComponentModel;
using System.Windows.Data;


namespace Morin.Wpf.ViewModels.Settings;

internal class TelevisionSettingsViewModel(IAppService appService) : Screen
{
    private readonly IAppService appService = appService;


    /// <summary>
    /// 通信运营商类型
    /// </summary>
    public static IEnumerable<NetworkCarrierType> NetworkCarrierTypes =>
        Enum.GetValues(typeof(NetworkCarrierType)).Cast<NetworkCarrierType>();

    private NetworkCarrierType _networkCarrierType;

    public NetworkCarrierType NetworkCarrierType
    {
        get => _networkCarrierType;
        set => _networkCarrierType = value;
    }

    private List<TvSourceModel>? _tVSources;
    private TvSourceModel? _tVSourceItem;

    public TvSourceModel? TVSourceItem
    {
        get => _tVSourceItem;
        set
        {
            _tVSourceItem = value;
            if (value != null) TVSourceDetails = [.. value.TvSourceDetails];
        }
    }

    public string? TVSourceGroupTitle { get; set; }
    public string? TVSourceDetailTitle { get; set; }
    public string? WebAddr { get; set; }

    public string? SearchContent { get; set; }
    public ICollectionView? TVCollectionView { get; set; }

    public BindableCollection<TvSourceDetailModel> TVSourceDetails { get; set; } = [];
    private TvSourceDetailModel _networkCarrier = new();

    public TvSourceDetailModel NetworkCarrier
    {
        get => _networkCarrier;
        set => _networkCarrier = value;
    }


    protected override void OnInitialActivate()
    {
        base.OnInitialActivate();

        _tVSources = [.. appService.GetTvSources()];

        TVCollectionView = CollectionViewSource.GetDefaultView(_tVSources);
        TVCollectionView.Filter = OnFilter;
    }

    private bool OnFilter(object o)
    {
        var result = false;
        if (o is TvSourceModel model)
        {
            if (string.IsNullOrEmpty(SearchContent)) return true;
            result = !string.IsNullOrEmpty(SearchContent)
                     && !string.IsNullOrEmpty(model.GroupTitle)
                     && model.GroupTitle.Contains(SearchContent);
        }

        return result;
    }

    public void Search()
    {
        TVCollectionView?.Refresh();
    }

    public void TVSourceOnDialogClosing(object sender, DialogClosingEventArgs e)
    {
        if (e.Parameter != null && e.Parameter.Equals(true))
        {
            var eq = _tVSources?.FirstOrDefault(x => x.GroupTitle != null && x.GroupTitle.Equals(TVSourceGroupTitle));

            if (eq == null)
            {
                var model = new TvSourceModel
                {
                    GroupTitle = TVSourceGroupTitle,
                    TvSourceDetails = []
                };
                appService.TvSourceAddOrUpdate(model);
                _tVSources?.Add(model);

                TVSourceGroupTitle = null;

                Search();
            }
        }
    }

    public void TVSourceDetailOnDialogClosing(object sender, DialogClosingEventArgs e)
    {
        if (e.Parameter != null && e.Parameter.Equals(true))
            if (TVSourceItem != null)
            {
                //  添加
                var networkCarrier = new TvSourceDetailModel
                {
                    Title = TVSourceDetailTitle,
                    WebAddr = WebAddr,
                    NetworkCarrierType = NetworkCarrierType
                };
                TVSourceDetails.Add(networkCarrier);
                TVSourceItem.TvSourceDetails = TVSourceDetails;
                //  更新
                appService.TvSourceAddOrUpdate(TVSourceItem);

                WebAddr = null;
                TVSourceDetailTitle = null;
            }
    }

    public void TVSourceDetailsRemove(TvSourceDetailModel networkCarrier)
    {
        TVSourceDetails.Remove(networkCarrier);
        //  更新
        if (TVSourceItem != null)
        {
            TVSourceItem.TvSourceDetails = TVSourceDetails;
            appService.TvSourceAddOrUpdate(TVSourceItem);
        }
    }

    public void TVSourceRemove(TvSourceModel model)
    {
        if (model != null && !string.IsNullOrEmpty(model.GroupTitle))
        {
            appService.TvSourceTryRemove(model.GroupTitle);
            _tVSources?.Remove(model);
        }
    }
}